package com.ruoyi.common.utils.appsdks;

import cn.hutool.core.collection.CollUtil;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.message.PushBatchDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ab785
 * createTime: 2021-12-7.
 * description: 个推推送工具类
 */
@Configuration
@ConditionalOnProperty(prefix = "push.getui", name = "enable", havingValue = "true")
public class PushGetuiUtils {
    private static UserApi userApi;
    private static PushApi pushApi;

    @PostConstruct
    public void init() {
        PushGetuiUtils.userApi = SpringUtils.getBean(UserApi.class);
        PushGetuiUtils.pushApi = SpringUtils.getBean(PushApi.class);
    }

    /**
     * 绑定用户
     */
    public static ApiResult<Void> bindUser(String clientId, String userName) {
        CidAliasListDTO listDTO = new CidAliasListDTO();
        listDTO.add(new CidAliasListDTO.CidAlias(clientId, userName));
        return userApi.bindAlias(listDTO);
    }

    /**
     * 推送消息到用户 通过别名
     */
    public static ApiResult<Map<String, Map<String, String>>> pushToUser(String title, String message, List<String> users) {
        PushBatchDTO batchDTO = new PushBatchDTO();
        List<PushDTO<Audience>> pushList = new ArrayList<>();
        batchDTO.setMsgList(pushList);

        for (String user : users) {
            // 设置接收人信息
            Audience audience = new Audience();
            audience.setAlias(CollUtil.newArrayList(user));
            //通知内容设置
            GTNotification notification = new GTNotification();
            notification.setTitle(title);
            notification.setBody(message);
            notification.setClickType("payload");
            notification.setPayload("payload");

            //消息设置
            PushMessage pushMessage = new PushMessage();
            pushMessage.setNotification(notification);
            //推送对象设置
            PushDTO<Audience> pushDTO = new PushDTO<>();
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            pushDTO.setAudience(audience);
            pushDTO.setPushMessage(pushMessage);
            pushList.add(pushDTO);
        }
        //推送
        return pushApi.pushBatchByAlias(batchDTO);
    }

    /**
     * 推送消息到用户 通过cid
     */
    public static ApiResult<Map<String, Map<String, String>>> pushToCid(String title, String message, List<String> cidList) {
        PushBatchDTO batchDTO = new PushBatchDTO();
        List<PushDTO<Audience>> pushList = new ArrayList<>();
        batchDTO.setMsgList(pushList);

        for (String cid : cidList) {
            // 设置接收人信息
            Audience audience = new Audience();
            audience.setCid(CollUtil.newArrayList(cid));
            //通知内容设置
            GTNotification notification = new GTNotification();
            notification.setTitle(title);
            notification.setBody(message);
            notification.setClickType("payload");
            notification.setPayload("payload");

            //消息设置
            PushMessage pushMessage = new PushMessage();
            pushMessage.setNotification(notification);
            //推送对象设置
            PushDTO<Audience> pushDTO = new PushDTO<>();
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            pushDTO.setAudience(audience);
            pushDTO.setPushMessage(pushMessage);
            pushList.add(pushDTO);
        }
        //推送
        return pushApi.pushBatchByCid(batchDTO);
    }

    /**
     * 推送透传消息给用户 通过别名
     */
    public static ApiResult<Map<String, Map<String, String>>> pushToUser(String jsonData, List<String> users) {
        PushBatchDTO batchDTO = new PushBatchDTO();
        List<PushDTO<Audience>> pushList = new ArrayList<>();
        batchDTO.setMsgList(pushList);

        for (String user : users) {
            // 设置接收人信息
            Audience audience = new Audience();
            audience.setAlias(CollUtil.newArrayList(user));

            //消息设置
            PushMessage pushMessage = new PushMessage();
            pushMessage.setTransmission(jsonData);

            //推送对象设置
            PushDTO<Audience> pushDTO = new PushDTO<>();
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            pushDTO.setAudience(audience);
            pushDTO.setPushMessage(pushMessage);
            pushList.add(pushDTO);
        }
        //推送
        return pushApi.pushBatchByAlias(batchDTO);
    }


    /**
     * 推送透传消息给用户 通过别名
     */
    public static ApiResult<Map<String, Map<String, String>>> pushToCid(String jsonData, List<String> cidList) {
        PushBatchDTO batchDTO = new PushBatchDTO();
        List<PushDTO<Audience>> pushList = new ArrayList<>();
        batchDTO.setMsgList(pushList);

        for (String cid : cidList) {
            // 设置接收人信息
            Audience audience = new Audience();
            audience.setCid(CollUtil.newArrayList(cid));

            //消息设置
            PushMessage pushMessage = new PushMessage();
            pushMessage.setTransmission(jsonData);

            //推送对象设置
            PushDTO<Audience> pushDTO = new PushDTO<>();
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            pushDTO.setAudience(audience);
            pushDTO.setPushMessage(pushMessage);
            pushList.add(pushDTO);
        }
        //推送
        return pushApi.pushBatchByCid(batchDTO);
    }

}
